RPM_NAME=dsms-deploy
RPM_VERSION=V1.0.0
RPM_RELEASE=V1.0
RPM_ARCH=x86_64
RPM_BUILD_DIR=$(shell pwd)/rpmbuild
RPM_SPEC_FILE=$(RPM_BUILD_DIR)/SPECS/$(RPM_NAME).spec

.PHONY: clean build

clean:
	rm -rf target
	rm -rf dist
	rm -rf $(RPM_BUILD_DIR)
build: clean
	mkdir -p $(RPM_BUILD_DIR)/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
	mkdir -p $(RPM_BUILD_DIR)/BUILD/$(RPM_NAME)/
	pyinstaller --onefile dsms-deploy.py
	cp dist/dsms-deploy $(RPM_BUILD_DIR)/BUILD/$(RPM_NAME)/
	cp config/cluster.conf $(RPM_BUILD_DIR)/BUILD/$(RPM_NAME)/
	cp config/add-node.conf $(RPM_BUILD_DIR)/BUILD/$(RPM_NAME)/
	cp config/depend_list $(RPM_BUILD_DIR)/BUILD/$(RPM_NAME)/
	cp config/whl/* $(RPM_BUILD_DIR)/BUILD/$(RPM_NAME)/
	cp ../VERSION $(RPM_BUILD_DIR)/BUILD/$(RPM_NAME)/

	sed -e "s/@RPM_NAME@/$(RPM_NAME)/g" \
	        -e "s/@RPM_VERSION@/$(RPM_VERSION)/g" \
	        -e "s/@RPM_RELEASE@/$(RPM_RELEASE)/g" \
	        -e "s/@RPM_ARCH@/$(RPM_ARCH)/g" \
        	-e "s|@RPM_BUILD_DIR@|$(RPM_BUILD_DIR)|g" \
	        config/$(RPM_NAME).spec > $(RPM_SPEC_FILE)

	rpmbuild -bb $(RPM_SPEC_FILE)

	mkdir target

	find $(RPM_BUILD_DIR)/RPMS -type f -name "*.rpm" -exec cp {} ./target \;

	rm -rf $(RPM_BUILD_DIR)

	rm -rf dist